﻿Public Class EDITPROVIDER
    Public Sub New(ByVal empid As String, ByVal usmk As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        publicUsmk = usmk
        publicEditingEmpid = empid
    End Sub

    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_MASPROVIDER As New PROVIDERSCLASS
    Dim publicUsmk As String
    Dim publicEditingEmpid As String

    Public Sub filterPrename()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(a.PRENAME AS CHAR(8)) AS PRENAME, a.FTPRENAME, b.SEXDESC FROM masprename a LEFT JOIN massex b ON a.sex = b.SEX WHERE a.STATUS = '1'"
        s = New FILTERCLASS(txtPrename, sql, "id,คำนำหน้าชื่อ,เพศ", "50,160,50", "1,1,1", "1,1,1")
        s.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    'Public Sub filterPrename(sex As String)
    '    Dim s As FILTERCLASS
    '    Dim sql As String = "SELECT CAST(a.PRENAME AS CHAR(8)) AS PRENAME, a.FTPRENAME, b.SEXDESC FROM masprename a LEFT JOIN massex b ON a.sex = b.SEX WHERE a.STATUS = '1' AND a.sex = '" & sex & "'"
    '    s = New FILTERCLASS(txtPrename, sql, "id,คำนำหน้าชื่อ,เพศ", "50,160,50", "1,1,1", "1,1,1")
    '    s.SetShowInDown = False
    '    s.SetShowBorder = True
    'End Sub

    Public Sub filterSex()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(sex AS CHAR(8)) as sex, sexdesc FROM massex WHERE `status` = '1'"
        s = New FILTERCLASS(txtSex, sql, "id เพศ,เพศ", "50,120", "1,1", "1,1")
        s.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterProtype()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(protype AS CHAR(8)) as protype, proname FROM masprotype WHERE `status` = '1'"
        s = New FILTERCLASS(txtProtype, sql, "id,ประเภทผู้ให้บริการ", "30,120", "1,1", "1,1")
        s.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterMasclinic()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(clinic AS CHAR(8)) as clinic, clinicname, cliniccode FROM masclinic WHERE `status` = '1'"
        s = New FILTERCLASS(txtClinic, sql, "id,ชื่อคลินิก,code", "30,120,50", "1,1,1", "1,1,1")
        s.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterMasclinic2()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(clinic AS CHAR(8)) as clinic, clinicname, cliniccode FROM masclinic WHERE `status` = '1'"
        s = New FILTERCLASS(txtDcl, sql, "id,ชื่อคลินิก,code", "30,120,50", "1,1,1", "1,1,1")
        s.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub queryProviderToForm()
        Dim objQueryProvider As New PROVIDERCLASS
        Dim sql As String = objQueryProvider.selectProviderInfo_
        sql += "WHERE h.empid = '" & publicEditingEmpid & "'"

        Dim dt As DataTable
        dt = connect.GetTable(sql)

        If dt.Rows(0)("บัญชีผู้ให้บริการ") IsNot DBNull.Value Then
            txtProvider.Text = dt.Rows(0)("บัญชีผู้ให้บริการ")
        Else
            txtProvider.Clear()
        End If

        If dt.Rows(0)("User Request") IsNot DBNull.Value Then
            txtUserRequest.Text = dt.Rows(0)("User Request")
        Else
            txtUserRequest.Clear()
        End If

        If dt.Rows(0)("User Request") IsNot DBNull.Value Then
            txtConfirmUserRequest.Text = dt.Rows(0)("User Request")
        Else
            txtConfirmUserRequest.Clear()
        End If

        If dt.Rows(0)("เลขบัตรประชาชน") IsNot DBNull.Value Then
            txtCID.Text = dt.Rows(0)("เลขบัตรประชาชน")
        Else
            txtCID.Clear()
        End If

        If dt.Rows(0)("Register No.") IsNot DBNull.Value Then
            txtRegisterNo.Text = dt.Rows(0)("Register No.")
        Else
            txtRegisterNo.Clear()
        End If

        If dt.Rows(0)("รหัสคำนำหน้า") IsNot DBNull.Value Then
            txtPrename.Text = dt.Rows(0)("คำนำหน้า")
            txtPrename.Tag = dt.Rows(0)("รหัสคำนำหน้า")
        Else
            txtPrename.Clear()
            txtPrename.Tag = Nothing
        End If

        If dt.Rows(0)("ชื่อ") IsNot DBNull.Value Then
            txtName.Text = dt.Rows(0)("ชื่อ")
        Else
            txtName.Clear()
        End If

        If dt.Rows(0)("นามสกุล") IsNot DBNull.Value Then
            txtLname.Text = dt.Rows(0)("นามสกุล")
        Else
            txtLname.Clear()
        End If

        If dt.Rows(0)("ชื่อ-นามสกุลอังกฤษ") IsNot DBNull.Value Then
            txtNameEn.Text = dt.Rows(0)("ชื่อ-นามสกุลอังกฤษ")
        Else
            txtNameEn.Clear()
        End If

        If dt.Rows(0)("ชื่อเล่น") IsNot DBNull.Value Then
            txtNname.Text = dt.Rows(0)("ชื่อเล่น")
        Else
            txtNname.Clear()
        End If

        If dt.Rows(0)("รหัสเพศ") IsNot DBNull.Value Then
            txtSex.Text = dt.Rows(0)("เพศ")
            txtSex.Tag = dt.Rows(0)("รหัสเพศ")
        Else
            txtSex.Clear()
            txtSex.Tag = Nothing
        End If

        If dt.Rows(0)("วันเกิด") IsNot DBNull.Value Then
            dtpBdate.Value = dt.Rows(0)("วันเกิด")
        Else
            dtpBdate.Value = Nothing
        End If

        If dt.Rows(0)("วันเริ่มทำงาน") IsNot DBNull.Value Then
            dtpStdate.Value = dt.Rows(0)("วันเริ่มทำงาน")
        Else
            dtpStdate.Value = Nothing
        End If

        If dt.Rows(0)("วันที่บรรจุ") IsNot DBNull.Value Then
            dtpAppdate.Value = dt.Rows(0)("วันที่บรรจุ")
        Else
            dtpAppdate.Value = Nothing
        End If

        If dt.Rows(0)("วันลาออก") IsNot DBNull.Value Then
            dtpLedate.Value = dt.Rows(0)("วันลาออก")
        Else
            dtpLedate.Value = Nothing
        End If

        If dt.Rows(0)("แพทย์") IsNot DBNull.Value Then
            If dt.Rows(0)("แพทย์") = True Then
                ckbF_doc.Checked = True

                linDcl.Visible = True
                txtDcl.Visible = True
                lblDcl.Visible = True
            Else
                ckbF_doc.Checked = False

                linDcl.Visible = False
                txtDcl.Visible = False
                lblDcl.Visible = False
            End If
        Else
            ckbF_doc.Checked = False
        End If

        If dt.Rows(0)("การเงิน") IsNot DBNull.Value Then
            If dt.Rows(0)("การเงิน") = True Then
                ckbF_cash.Checked = True
            Else
                ckbF_cash.Checked = False
            End If
        Else
            ckbF_cash.Checked = False
        End If

        If dt.Rows(0)("รหัสประเภทผู้ให้บริการ") IsNot DBNull.Value Then
            txtProtype.Text = dt.Rows(0)("ประเภทผู้ให้บริการ")
            txtProtype.Tag = dt.Rows(0)("รหัสประเภทผู้ให้บริการ")
        Else
            txtProtype.Clear()
            txtProtype.Tag = Nothing
        End If

        If dt.Rows(0)("รหัสห้องตรวจ") IsNot DBNull.Value Then
            txtClinic.Text = dt.Rows(0)("ห้องตรวจ")
            txtClinic.Tag = dt.Rows(0)("รหัสห้องตรวจ")
        Else
            txtClinic.Clear()
            txtClinic.Tag = Nothing
        End If

        If dt.Rows(0)("รหัสคลินิกประจำตัว") IsNot DBNull.Value Then
            txtDcl.Text = dt.Rows(0)("คลินิกประจำตัว")
            txtDcl.Tag = dt.Rows(0)("รหัสคลินิกประจำตัว")
        Else
            txtDcl.Clear()
            txtDcl.Tag = Nothing
        End If

        If dt.Rows(0)("การเปิดใช้งาน") IsNot DBNull.Value Then
            If dt.Rows(0)("การเปิดใช้งาน") = 1 Then
                ckbHospempStatus.Checked = True
            Else
                ckbHospempStatus.Checked = False
            End If
        Else
            ckbHospempStatus.Checked = False
        End If
    End Sub

    Private Sub ADDPROVIDER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        filterSex()
        filterPrename()
        filterMasclinic()
        filterMasclinic2()
        filterProtype()

        queryProviderToForm()
    End Sub

    Private Sub ckbF_doc_CheckedChanged(sender As Object, e As EventArgs) Handles ckbF_doc.CheckedChanged
        If ckbF_doc.Checked = True Then
            linDcl.Visible = True
            txtDcl.Visible = True
            lblDcl.Visible = True
        Else
            linDcl.Visible = False
            txtDcl.Visible = False
            lblDcl.Visible = False
            txtDcl.Text = Nothing
            txtDcl.Tag = Nothing
        End If
    End Sub


    '--------check password----------
    Private Sub UMAINPASSTxt_TextChanged(sender As Object, e As EventArgs) Handles txtUserRequest.TextChanged
        If txtUserRequest.Text = txtConfirmUserRequest.Text Then
            passwordMatch.Visible = False
            btnEditProvider.Enabled = True
        Else
            passwordMatch.Visible = True
            btnEditProvider.Enabled = False
        End If
    End Sub

    Private Sub CONFIRMPASSTxt_TextChanged(sender As Object, e As EventArgs) Handles txtConfirmUserRequest.TextChanged
        If txtUserRequest.Text = txtConfirmUserRequest.Text Then
            passwordMatch.Visible = False
            btnEditProvider.Enabled = True
        Else
            passwordMatch.Visible = True
            btnEditProvider.Enabled = False
        End If
    End Sub

    Private Sub editProvider_Click_1(sender As Object, e As EventArgs) Handles btnEditProvider.Click
        'If main.PROVIDERPOLICIES.checklogin_user_ = True Then
        '    If PROVIDER_MASPROVIDER.PUPD_ = True Then
        Dim editStat As Integer
        editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
        If editStat = 1 Then
            If Convert.ToString(txtPrename.Tag) IsNot Nothing And txtName.Text.ToString.Length > 0 And txtLname.Text.Length.ToString > 0 And txtNname.Text.Length.ToString > 0 And txtUserRequest.Text.Length.ToString > 0 And txtConfirmUserRequest.Text.Length.ToString > 0 Then
                Dim objEditData As New PROVIDERCLASS
                objEditData.EMPID_ = publicEditingEmpid
                objEditData.CID_ = txtCID.Text
                objEditData.REGISTERNO_ = txtRegisterNo.Text
                objEditData.PROVIDER_ = txtProvider.Text
                objEditData.PRENAME_ = txtPrename.Tag
                objEditData.NAME_ = txtName.Text
                objEditData.LNAME_ = txtLname.Text
                objEditData.NNAME_ = txtNname.Text
                objEditData.NAME_EN_ = txtNameEn.Text

                If Convert.ToString(txtSex.Tag) IsNot Nothing Then
                    objEditData.SEX_ = txtSex.Tag
                Else
                    objEditData.SEX_ = 0
                End If

                If Convert.ToString(txtClinic.Tag) IsNot Nothing Then
                    objEditData.CLINIC_ = txtClinic.Tag
                Else
                    objEditData.CLINIC_ = 0
                End If

                If Convert.ToString(txtDcl.Tag) IsNot Nothing Then
                    objEditData.DCL_ = txtDcl.Tag
                Else
                    objEditData.DCL_ = 0
                End If

                If Convert.ToString(txtProtype.Tag) IsNot Nothing Then
                    objEditData.PROTYPE_ = txtProtype.Tag
                Else
                    objEditData.DCL_ = 0
                End If

                objEditData.BDATE_ = DateAdd(DateInterval.Year, -543, dtpBdate.Value).ToString("yyyy/MM/dd")
                objEditData.APPDATE_ = DateAdd(DateInterval.Year, -543, dtpAppdate.Value).ToString("yyyy/MM/dd")
                objEditData.STARTDATE_ = DateAdd(DateInterval.Year, -543, dtpStdate.Value).ToString("yyyy/MM/dd")
                objEditData.LEDATE_ = DateAdd(DateInterval.Year, -543, dtpLedate.Value).ToString("yyyy/MM/dd")
                objEditData.F_CASH_ = ckbF_cash.Checked
                objEditData.F_DR_ = ckbF_doc.Checked
                objEditData.F_LOGIN_ = ckbF_cash.Checked
                objEditData.USREQ_ = txtUserRequest.Text
                objEditData.USMK_ = publicUsmk
                objEditData.providerstatus_ = ckbHospempStatus.Checked


                If objEditData.editproviderData() = True Then
                    DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                End If
            Else
                MsgBox("โปรดกรุณาระบุข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
            End If

            'SelectDatagridprovider()

            'cleardata()
            'End If
            ''ADDPROVIDER.Enabled = True
            '    Else
            'MsgBox(PROVIDER_MASPROVIDER.PUPDMSG_)
            '    End If
            'Else
            'TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            'main.txtUserRequest.Focus()
        End If
    End Sub

   
End Class